﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Sales;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.Sales;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Finance
{
    public partial class frmManageExpensesClaims : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private SalesFeeEntity mSelectedSalesFee;
        #endregion

        #region Constructor
        public frmManageExpensesClaims()
        {
            InitializeComponent();
            if (CommonParameter.CurrentUser.PersonEntity != null)
            {
                txtPerson.Tag = CommonParameter.CurrentUser.PersonEntity;
                txtPerson.Text = CommonParameter.CurrentUser.PersonEntity.NameCn;
            }
        }
        public frmManageExpensesClaims(MstCustomerEntity customer)
        {
            InitializeComponent();
            txtCustomer.Text = customer.CustomerName;
            txtCustomer.Tag = customer;
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "SFS", true);

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "SFS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colFEE_TYPE, "FEE", true);
        }
        private string GetCondition()
        {
            string condition = "";
            if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            {
                MstCustomerEntity customer = (MstCustomerEntity)txtCustomer.Tag;
                condition += " AND F.CUSTOMER_ID = '" + customer.CustomerId + "' ";
            }
            if (dtpFeeDate.EditValue != null)
            {
                condition += " AND F.FEE_END_DATE >= CONVERT(DATETIME,'" + dtpFeeDate.EditValue.ToString() + " 00:00:00')";
            }
            if (dtpFeeDateTo.EditValue != null)
            {
                condition += " AND F.FEE_FROM_DATE <= CONVERT(DATETIME,'" + dtpFeeDateTo.EditValue.ToString() + " 23:59:59')";
            }
            if (mCanQueryOtherPerson)
            {
                if (txtPerson.Tag != null && txtPerson.Tag.GetType() == typeof(MstPersonEntity))
                {
                    MstPersonEntity entity = (MstPersonEntity)txtPerson.Tag;
                    condition += " AND F.PERSON_ID = '" + entity.PersonId + "' ";
                }
            }
            else
            {
                condition += " AND F.PERSON_ID = '" + CommonParameter.CurrentUser.PersonId + "' ";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND F.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "P.NAME_CN, C.CUSTOMER_NAME, F.FEE_CODE";
            DataTable dt = SalesFeeBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        private void ResetButtonsEnable()
        {
            btnModify.Enabled = false;
            btnDelete.Enabled = false;
            btnSubmit.Enabled = false;
            btnApprove.Enabled = false;
            btnExpense.Enabled = false;
            if (mSelectedSalesFee == null)
            {
                return;
            }
            if (mSelectedSalesFee.Status == SalesFeeStatus.SFS01.ToString()
                || mSelectedSalesFee.Status == SalesFeeStatus.SFS03.ToString())
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
                btnApprove.Enabled = true;
            }
            else if (mSelectedSalesFee.Status == SalesFeeStatus.SFS02.ToString())
            {
                btnSubmit.Enabled = true;
            }
            else if (mSelectedSalesFee.Status == SalesFeeStatus.SFS05.ToString())
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
            }
            else if (mSelectedSalesFee.Status == SalesFeeStatus.SFS04.ToString())
            {
                btnExpense.Enabled = true;
            }
        }

        /// <summary>
        /// 判断用户时候可以查询其它用户的资料
        /// </summary>
        private void CheckPersonRight()
        {
            if (CommonParameter.CurrentUser == null)
            {
                return;
            }
            if (CommonParameter.CurrentUser.PersonId == "1")
            {
                mCanQueryOtherPerson = true;
                return;
            }
            else
            {
                //1=总经理；2=财务；3=人事；4=销售
                if (CommonParameter.CurrentUser.UserRoleCategory.Contains("1") || CommonParameter.CurrentUser.UserRoleCategory.Contains("2"))
                {
                    mCanQueryOtherPerson = true;
                }
            }
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            CheckPersonRight();
            BindComboBox();
            BindDataGrid();
            DataTable dt = gvMain.DataSource as DataTable;
            if (dt.Rows.Count == 0)
            {
                ResetButtonsEnable();
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedSalesFee = new SalesFeeEntity(dr);
            ResetButtonsEnable();
        }

        protected override void Click_View()
        {
            if (mSelectedSalesFee == null)
            {
                MessageBox.Show("请选择一个销售费用！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainExpensesClaims frm = new frmMaintainExpensesClaims();
            frm.ThisActionType = ActionType.View;
            frm.SelectedSalesFee = mSelectedSalesFee;
            frm.ShowDialog();
        }

        protected override void Click_New()
        {
            frmMaintainExpensesClaims frm = new frmMaintainExpensesClaims();
            if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            {
                frm = new frmMaintainExpensesClaims((MstCustomerEntity)txtCustomer.Tag);
            }
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Delete()
        {
            if (mSelectedSalesFee == null)
            {
                MessageBox.Show("请选择一个销售费用！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中销售费用？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedSalesFee.Status = SalesFeeStatus.SFS07.ToString();
                mSelectedSalesFee.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedSalesFee.LastModifiedDate = DateTime.Now;
                bool successful = SalesFeeBs.UpdateSalesFee(mSelectedSalesFee);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Submit()
        {
            if (mSelectedSalesFee == null)
            {
                MessageBox.Show("请选择一个销售费用！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainExpensesClaims frm = new frmMaintainExpensesClaims();
            frm.ThisActionType = ActionType.Approve;
            frm.ThisApproveType = ApproveType.CEO;
            frm.SelectedSalesFee = mSelectedSalesFee;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedSalesFee == null)
            {
                MessageBox.Show("请选择一个销售费用！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainExpensesClaims frm = new frmMaintainExpensesClaims();
            frm.ThisActionType = ActionType.Approve;
            frm.ThisApproveType = ApproveType.DM;
            frm.SelectedSalesFee = mSelectedSalesFee;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedSalesFee == null)
            {
                MessageBox.Show("请选择一个销售费用！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainExpensesClaims frm = new frmMaintainExpensesClaims();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedSalesFee = mSelectedSalesFee;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void txtCustomer_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainCustomerQuery frm = new frmMaintainCustomerQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedCustomer != null)
            {
                txtCustomer.Text = frm.SelectedCustomer.CustomerName;
                txtCustomer.Tag = frm.SelectedCustomer;
            }
            else
            {
                txtCustomer.Text = "";
                txtCustomer.Tag = null;
            }
        }

        private void txtPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (!mCanQueryOtherPerson)
            {
                return;
            }
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtPerson.Text = frm.SelectedPerson.NameCn;
                txtPerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtPerson.Text = "";
                txtPerson.Tag = null;
            }
        }
        
        private void btnExpense_Click(object sender, EventArgs e)
        {
            if (mSelectedSalesFee == null)
            {
                MessageBox.Show("请选择一个销售费用！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainExpensesClaims frm = new frmMaintainExpensesClaims();
            frm.ThisActionType = ActionType.Approve;
            frm.ThisApproveType = ApproveType.FIN;
            frm.SelectedSalesFee = mSelectedSalesFee;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        #endregion

    }
}
